<!--
 * @Author: Bennty
 * @Date: 2024-11-18 16:01:36
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-11-21 14:11:27
 * @Description: 请填写简介
-->
<template>
    <div class="p-2">
        <transition
            :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave"
        >
            <div class="search">
                <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                    <el-form-item label="选择日期">
                        <el-col :span="11">
                            <el-date-picker v-model="queryParams.startDate" placeholder="开始日期" format="YYYY/MM/DD" value-format="YYYY-MM-DD"></el-date-picker>
                        </el-col>
                        <el-col :span="2" class="text-center">
                            <span class="text-gray-500">-</span>
                        </el-col>
                        <el-col :span="11">
                            <el-date-picker v-model="queryParams.end" placeholder="结束日期" format="YYYY/MM/DD" value-format="YYYY-MM-DD"></el-date-picker>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="入库" prop="inStatus">
                        <el-select v-model="queryParams.inStatus" clearable>
                            <el-option label="未入库" value="0"></el-option>
                            <el-option label="部分入库" value="1"></el-option>
                            <el-option label="已入库" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="模糊搜索" prop="packingName">
                        <el-input v-model="queryParams.packingName" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <el-table :data="packageOrderList">
                <el-table-column label="制单日期" prop="createTime" align="center"></el-table-column>
                <el-table-column label="生产单号" prop="packingProductCode" align="center"></el-table-column>
                <el-table-column label="包装件名称" prop="packingName" width="220px" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="生产数量" prop="produceNumber" align="center"></el-table-column>
                <el-table-column label="已入库数量" prop="inNumber" align="center"></el-table-column>
                <el-table-column label="打印状态" align="center">
                    <template #default="scope">
                        <el-button v-if="scope.row.printStatus == '0'" link type="danger">未打印</el-button>
                        <span class="primary" v-else>已打印</span>
                    </template>
                </el-table-column>
                <el-table-column label="入库状态" align="center">
                    <template #default="scope">
                        <span class="alert" v-if="scope.row.inStatus == '0'">未入库</span>
                        <span class="normal" v-else-if="scope.row.inStatus == '1'">部分入库</span>
                        <span class="normal" v-else-if="scope.row.inStatus == '2'">已入库</span>
                    </template>
                </el-table-column>
                <el-table-column label="配料状态" align="center">
                    <template #default="scope">
                        <el-button type="danger" link v-if="scope.row.inStatus == '0'" @click="handleClickApplyIngredients(scope.row)">未配料</el-button>
                        <span class="normal" v-if="scope.row.inStatus == '1'">已配料</span>
                    </template>
                </el-table-column>
                <el-table-column label="交货日期" prop="deliveryDate" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="220px">
                    <template #default="scope">
                        <el-tooltip content="编辑" placement="top">
                            <el-button link :style="{fontSize:'15px'}" type="primary" icon="Edit" @click="handleClickEdit(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除">
                            <el-button link :style="{fontSize:'15px'}" type="danger" icon="Delete" @click="handleClickDelete(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="订单详情">
                            <el-button link :style="{fontSize:'15px'}" type="warning" icon="Document" @click="handleClickCheckOrder(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="入库">
                            <el-button link :style="{fontSize:'15px'}" type="success" icon="Back" @click="handleClickInStore(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="配料申请">
                            <el-button link :style="{fontSize:'15px'}" icon="ChatLineSquare"></el-button>
                        </el-tooltip>
                        <el-tooltip content="转采购">
                            <el-button link :style="{fontSize:'15px'}" type="primary" icon="Goods"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                @pagination="getPackageList" />
        </el-card>
        <editPackageOrder :visible="editPackageOrderVisible" :orderDetail="orderDetails" @submit="handleSubmit" @close="handleCloseEdit" />
        <checkOrderDetails :visible="checkPackageOrderVisible" :orderDetails="orderDetails" @close="handleCloseCheckOrder" />
        <packageInStore :visible="packageInStoreVisible" :orderDetails="orderDetails" @submit="handleSubmit" @close="handleCloseInStore" />
        <applyIngredients :visible="applyIngredientsVisible" :orderDetails="orderDetails" @close="handleCloseApplyIngredients" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { QueryPackageType,PackageVO } from '@/api/sj/productManagement/packageOrder/types';
import { getPackageListApi, getPackageOrderDetailApi,deletePackageOrderApi,getPackageProduceOrderDetailApi, getApplyIngredientsDetailApi } from '@/api/sj/productManagement/packageOrder/index';
import editPackageOrder from './components/editPackageOrder.vue';
import checkOrderDetails from './components/checkOrderDetails.vue';
import packageInStore from './components/packageInStore.vue';
import applyIngredients from './components/applyIngredients.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<QueryPackageType>>({
    queryParams:{
        startDate: undefined,
        endDate: undefined,
        packingName: undefined,
        inStatus: undefined,
        pageSize: 30,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);
const packageOrderList = ref<PackageVO[]>();
const totalData = ref(0);
const loading = ref(false);
const orderDetails = ref<any>();

const editPackageOrderVisible = ref(false);
const checkPackageOrderVisible = ref(false);
const packageInStoreVisible = ref(false);
const applyIngredientsVisible = ref(false);

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getPackageList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = undefined;
    queryParams.value.endDate = undefined;
    handleQuery();
};

const getPackageList = async () => {
    loading.value = true;
    const {code, rows, total} = await getPackageListApi(queryParams.value);
    if(code == 200){
        packageOrderList.value = rows;
        totalData.value = total;
        loading.value = false;
    };
};

/** 提交成功的操作 */
const handleSubmit = (result: boolean) => {
    if(result){
        proxy?.$modal.msgSuccess('成功');
        getPackageList();
    };
};

const handleClickEdit = async (row: PackageVO) => {
    const {code, data} = await getPackageOrderDetailApi(row.id);
    if(code == 200){
        orderDetails.value = data;
        editPackageOrderVisible.value = true;
    };
};

const handleCloseEdit = () => {
    editPackageOrderVisible.value = false;
    orderDetails.value = {};
};

const handleClickDelete = async (row: PackageVO) => {
    await proxy?.$modal.confirm("此条信息将彻底删除，确认么？");
    const { code } = await deletePackageOrderApi(row.id);
    if(code == 200){
        proxy?.$modal.msgSuccess("删除包装件订单成功！");
        getPackageList();
    };
};

const handleClickCheckOrder = async (row: PackageVO) => {
    const {code, data} = await getPackageProduceOrderDetailApi(row.id);
    if(code == 200){
        orderDetails.value = data;
        checkPackageOrderVisible.value = true;
    };
};

const handleCloseCheckOrder = () => {
    checkPackageOrderVisible.value = false;
};

const handleClickInStore = async (row: PackageVO) => {
    const {code, data} = await getPackageOrderDetailApi(row.id);
    if(code == 200){
        orderDetails.value = data;
        packageInStoreVisible.value = true;
    };
};

const handleCloseInStore = () => {
    packageInStoreVisible.value = false;
};

const handleClickApplyIngredients = async (row: PackageVO) => {
    const { code, data } = await getApplyIngredientsDetailApi(row.id);
    if(code == 200){
        orderDetails.value = data;
        applyIngredientsVisible.value = true;
    };
};

const handleCloseApplyIngredients = () => {
    applyIngredientsVisible.value = false;
};

onMounted(()=>{
    getPackageList();
});

</script>
<style lang="scss" scoped>

</style>